import { DefaultStage } from "../default-stage";
import { RawAssetsName, RawAssetsUrl } from "../../raw-assets-info";
import { GradientSkybox } from 'babylon-lib/preset/gradient-skybox';
import { Debug } from "babylon-lib/gui/control/debug";
import { Axis, Color3, Vector2, Vector3 } from "@babylonjs/core/Maths/math";
import { GravityRoaming } from "babylon-lib/preset/gravity-roaming";
import { Helper } from "babylon-lib/tool/helper";
import { AbstractControl } from 'babylon-lib/gui/control/abstract-control';
import { MeshBuilder } from "@babylonjs/core/Meshes/meshBuilder";
import { TransformNode } from "@babylonjs/core/Meshes/transformNode";
import { FreezeCamera } from 'babylon-lib/misc/freeze-camera';
import { Scene } from "@babylonjs/core/scene";
import { ArcRotateCamera } from "@babylonjs/core/Cameras/arcRotateCamera";
import { DetailView } from "babylon-lib/preset/detail-view";
import * as Data from '../../data/data';
import { StandardMaterial } from "@babylonjs/core/Materials/standardMaterial";
import { RenderTargetTexture } from "@babylonjs/core/Materials/Textures/renderTargetTexture";


class GenerateComponent {
    public static initialize(stage: DefaultStage): void {
        this.setMovement(stage);
        // this.testDebug(stage);
        this.initializeSky(stage);
        this.test(stage);
        this.testDetailView(stage);
    }

    private static testDetailView(stage: DefaultStage): void {
        const scn = stage.mainScene;
        const cam = new ArcRotateCamera('detail-camera', -1.57, 1.57, 10, new Vector3(0, 0, 0), scn, false);
        cam.detachControl();
        const dv = new DetailView('hhhdjjd', scn, scn, {
            camera: cam,
            billboard: TransformNode.BILLBOARDMODE_Y,
            textureResolution: new Vector2(128, 128),
            refreshRate: 10,
        });

        const nodes = stage.loader(stage.mainScene).getMeshTaskInfo(RawAssetsUrl.changjing)?.loadedTransformNodes!;
        const model = Helper.nodeInArray('changjing', nodes, undefined, true)!;
        dv.addRenderTarget(...scn.meshes);

        Data.test.detailView = dv;

        setInterval(()=>{
            cam.alpha+=0.01;
        },30);
    }

    private static test(stage: DefaultStage): void {
        const scene = stage.mainScene;
        const nodes = stage.loader(scene).getMeshTaskInfo(RawAssetsUrl.changjing)?.loadedTransformNodes!;
        const model = Helper.nodeInArray('changjing', nodes, undefined, true)!;
        const container = new TransformNode('model-container', scene);
        model.setParent(container);
        console.log(nodes);

        const randP = () => {
            return Math.random() * 200 - 100;
        };
        const b = MeshBuilder.CreateBox('hhhsjkdhfjs', { size: 2 }, scene);
        b.isPickable = false;
        b.isVisible = false;
        for (let i = 0; i < 100; i++) {
            const ib = b.createInstance('m-' + i);
            ib.isPickable = false;
            ib.position.set(randP(), randP(), randP());
        }

    }

    private static setMovement(stage: DefaultStage): void {
        const scene = stage.mainScene;
        Helper.setSceneMesh(scene, 'isPickable', false, [AbstractControl.namePostfix]);
        Helper.setSceneMesh(scene, 'checkCollisions', false);
        const roaming = new GravityRoaming(scene, {
            floorMeshKey: RawAssetsName.ground,
            obstacleMeshKey: RawAssetsName.bound,
            autoUpdate: true,
        });

    }

    private static testDebug(stage: DefaultStage): void {
        Debug.initialize(stage.mainScene, {
            scattered: true,
            offset: new Vector3(2, 0, 0),
            lockToCamera: false
        });
    }

    private static initializeSky(stage: DefaultStage): GradientSkybox {
        const sky = new GradientSkybox('sky', stage.mainScene, {
            values: [0, 0.5, 0.65],
            colors: ['#00afdb', '#b3f0ff', '#ff9a52'],
            size: 500,
        });
        sky.mesh.isPickable = false;
        return sky;
    }

}

export { GenerateComponent };
